


#include <iostream>
#include <list>
#include <vector>
using namespace std;

class Solution 
{
public:
    int majorityElement(vector<int>& nums)
    {
        int res = nums[0], cnt = 1;

        for (int i = 1, n = nums.size(); i < n; ++i)
        {
            if (res == nums[i])
                ++cnt;
            else
            {
                if (0 == --cnt)
                {
                    res = nums[i];
                    cnt = 1;
                }
            }
        }

        return res;
    }

    int LastRemaining_Solution(int n, int m)
    {
        list<int> ls;
        for (int i = 0; i < n; ++i)
        {
            ls.push_back(i);
        }

        int cnt = 1;
        for (auto it = ls.begin(); ls.size() != 1;)
        {
            if (cnt == m)
            {
                it = ls.erase(it);
                cnt = 1;
            }
            else
            {
                ++cnt;
                ++it;
            }
            if (it == ls.end())
                it = ls.begin();
        }

        return ls.front();
    }

    void Test1()
    {
        int q = 0;
        cin >> q;
        int a = 0, b = 0, c = 0;

        while (q--)
        {
            int res = 0;
            cin >> a >> b >> c;

            while (a && b && c)
            {
                res += 2;
                --a, --b, --c;
            }

            if (b >= 2)
                res += b - 1;

            cout << res << endl;
        }
    }

    int res = 0, cnt = 0;
    int n = 0, m = 0;
    int dx[4] = { -1, 1, 0, 0 };
    int dy[4] = { 0, 0, -1, 1 };
    int rotApple(vector<vector<int> >& grid)
    {
        int cntgood = 0, cntbad = 0;
        vector<vector<int>> vis;
        n = grid.size(), m = grid[0].size();

        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < m; ++j)
            {
                if (1 == grid[i][j])
                    ++cntgood;
                if (2 == grid[i][j])
                    ++cntbad;
            }
        }
        if (0 == cntbad && cntgood)
            return -1;

        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < m; ++j)
            {
                if (2 == grid[i][j])
                {
                    res = 0;
                    vis = grid;
                    cnt = cntgood;

                    _bfs(grid, vis, i, j);
                    if (0 == cnt)
                        return res;
                }
            }
        }

        return -1;
    }

private:
    void _bfs(const vector<vector<int> >& grid, vector<vector<int>>& vis, int i, int j)
    {
        int flag = 1;
        for (int k = 0; k < 4; ++k)
        {
            int x = i + dx[k], y = j + dy[k];

            if (x >= 0 && x < n && y >= 0 && y < m && 1 == vis[x][y])
            {
                flag = 0;
                --cnt;
                vis[x][y] = 2;
            }
        }
        if (flag)
            return;

        ++res;
        for (int k = 0; k < 4; ++k)
        {
            int x = i + dx[k], y = j + dy[k];

            if (x >= 0 && x < n && y >= 0 && y < m && vis[x][y])
            {
                _bfs(grid, vis, x, y);
            }
        }
    }
};

void Test1()
{
    vector<vector<int>> vv1 = { {2,1,0},{1,0,1},{0,0,0 } };
    vector<vector<int>> vv2 = { {2, 1, 1}, { 1, 0, 1 }, { 1, 1, 1 } };
    vector<vector<int>> vv3 = { {2, 1, 1, 1}, { 1, 1, 1, 0 } };
    vector<vector<int>> vv4 = { {2, 2, 1, 1}};
    vector<vector<int>> vv5 = { {2, 1, 1}, { 1, 1, 1 }, { 1, 1, 0} };


    cout << Solution().rotApple(vv5) << endl;
}

int main()
{
    Test1();

	return 0;
}